/************************************************************************************/
/*  Program name CAPPOEXT_CONTROL_RULES.sql                              */
/*  Creater:  CHAN BILLY KA KEUNG (PCCW - UniHub)                                   */
/*************   History   *************************                                */
/* 24 MAY 2006				       First created                                    */
/************************************************************************************/
SET VERIFY OFF;
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPPOEXT_CONTROL_RULES';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPPO_CONTROL_RULES.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO_Name		VARCHAR2(100);
	lv_NGO_Count	NUMBER;
   
	CURSOR Cur_F(p_NGO VARCHAR2) IS
		SELECT A.*
		FROM PO_CONTROL_RULES A,
			PO_CONTROL_GROUPS_ALL B,
			HR_ALL_ORGANIZATION_UNITS C
		WHERE A.CONTROL_GROUP_ID = B.CONTROL_GROUP_ID
		AND B.ORG_ID = C.ORGANIZATION_ID
		AND Substr(C.NAME,4,4) = lv_NGO_Name;
		
BEGIN
	lv_NGO_Name		:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	Substr(NAME,4,4)	=	lv_NGO_Name;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO_Name) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.CONTROL_RULE_ID        ||CHR(9)||
				C1.CONTROL_GROUP_ID       ||CHR(9)||
				C1.LAST_UPDATE_DATE       ||CHR(9)||
				C1.LAST_UPDATED_BY        ||CHR(9)||
				C1.LAST_UPDATE_LOGIN      ||CHR(9)||
				C1.CREATION_DATE          ||CHR(9)||
				C1.CREATED_BY             ||CHR(9)||
				C1.PROGRAM_UPDATE_DATE    ||CHR(9)||
				C1.REQUEST_ID             ||CHR(9)||
				C1.PROGRAM_APPLICATION_ID ||CHR(9)||
				C1.PROGRAM_ID             ||CHR(9)||
				C1.RULE_TYPE_CODE         ||CHR(9)||
				C1.OBJECT_CODE            ||CHR(9)||
				C1.AMOUNT_LIMIT           ||CHR(9)||
				C1.LOCATION_ID            ||CHR(9)||
				C1.SEGMENT14_HIGH         ||CHR(9)|| 
				C1.INACTIVE_DATE          ||CHR(9)||
				C1.SEGMENT12_LOW          ||CHR(9)|| 
				C1.SEGMENT12_HIGH         ||CHR(9)|| 
				C1.SEGMENT1_LOW           ||CHR(9)|| 
				C1.SEGMENT13_LOW          ||CHR(9)|| 
				C1.SEGMENT1_HIGH          ||CHR(9)|| 
				C1.SEGMENT13_HIGH         ||CHR(9)|| 
				C1.SEGMENT2_LOW           ||CHR(9)|| 
				C1.SEGMENT14_LOW          ||CHR(9)|| 
				C1.SEGMENT2_HIGH          ||CHR(9)|| 
				C1.SEGMENT3_LOW           ||CHR(9)|| 
				C1.SEGMENT15_LOW          ||CHR(9)|| 
				C1.SEGMENT3_HIGH          ||CHR(9)|| 
				C1.SEGMENT15_HIGH         ||CHR(9)|| 
				C1.SEGMENT4_LOW           ||CHR(9)|| 
				C1.SEGMENT16_LOW          ||CHR(9)|| 
				C1.SEGMENT4_HIGH          ||CHR(9)|| 
				C1.SEGMENT16_HIGH         ||CHR(9)|| 
				C1.SEGMENT5_LOW           ||CHR(9)|| 
				C1.SEGMENT17_LOW          ||CHR(9)|| 
				C1.SEGMENT5_HIGH          ||CHR(9)|| 
				C1.SEGMENT17_HIGH         ||CHR(9)|| 
				C1.SEGMENT6_LOW           ||CHR(9)|| 
				C1.SEGMENT18_LOW          ||CHR(9)|| 
				C1.SEGMENT6_HIGH          ||CHR(9)|| 
				C1.SEGMENT18_HIGH         ||CHR(9)|| 
				C1.SEGMENT7_LOW           ||CHR(9)|| 
				C1.SEGMENT19_LOW          ||CHR(9)|| 
				C1.SEGMENT7_HIGH          ||CHR(9)|| 
				C1.SEGMENT19_HIGH         ||CHR(9)|| 
				C1.SEGMENT8_LOW           ||CHR(9)|| 
				C1.SEGMENT20_LOW          ||CHR(9)|| 
				C1.SEGMENT8_HIGH          ||CHR(9)|| 
				C1.SEGMENT20_HIGH         ||CHR(9)|| 
				C1.SEGMENT9_LOW           ||CHR(9)|| 
				C1.SEGMENT21_LOW          ||CHR(9)|| 
				C1.SEGMENT9_HIGH          ||CHR(9)|| 
				C1.SEGMENT10_LOW          ||CHR(9)|| 
				C1.SEGMENT21_HIGH         ||CHR(9)|| 
				C1.SEGMENT10_HIGH         ||CHR(9)|| 
				C1.SEGMENT22_LOW          ||CHR(9)|| 
				C1.SEGMENT11_LOW          ||CHR(9)|| 
				C1.SEGMENT22_HIGH         ||CHR(9)|| 
				C1.SEGMENT11_HIGH         ||CHR(9)|| 
				C1.SEGMENT23_LOW          ||CHR(9)|| 
				C1.SEGMENT23_HIGH         ||CHR(9)|| 
				C1.SEGMENT24_LOW          ||CHR(9)|| 
				C1.SEGMENT24_HIGH         ||CHR(9)|| 
				C1.SEGMENT25_LOW          ||CHR(9)|| 
				C1.SEGMENT25_HIGH         ||CHR(9)|| 
				C1.SEGMENT26_LOW          ||CHR(9)|| 
				C1.SEGMENT26_HIGH         ||CHR(9)|| 
				C1.SEGMENT27_LOW          ||CHR(9)|| 
				C1.SEGMENT27_HIGH         ||CHR(9)|| 
				C1.SEGMENT28_LOW          ||CHR(9)|| 
				C1.SEGMENT28_HIGH         ||CHR(9)|| 
				C1.SEGMENT29_LOW          ||CHR(9)|| 
				C1.SEGMENT29_HIGH         ||CHR(9)|| 
				C1.SEGMENT30_LOW          ||CHR(9)|| 
				C1.SEGMENT30_HIGH         ||CHR(9)|| 
				C1.ATTRIBUTE1             ||CHR(9)|| 
				C1.ATTRIBUTE2             ||CHR(9)|| 
				C1.ATTRIBUTE3             ||CHR(9)|| 
				C1.ATTRIBUTE4             ||CHR(9)|| 
				C1.ATTRIBUTE5             ||CHR(9)|| 
				C1.ATTRIBUTE6             ||CHR(9)|| 
				C1.ATTRIBUTE7             ||CHR(9)|| 
				C1.ATTRIBUTE8             ||CHR(9)|| 
				C1.ATTRIBUTE9             ||CHR(9)|| 
				C1.ATTRIBUTE10            ||CHR(9)|| 
				C1.ATTRIBUTE11            ||CHR(9)|| 
				C1.ATTRIBUTE12            ||CHR(9)|| 
				C1.ATTRIBUTE13            ||CHR(9)|| 
				C1.ATTRIBUTE14            ||CHR(9)|| 
				C1.ATTRIBUTE15            ||CHR(9)|| 
				C1.ATTRIBUTE_CATEGORY     
			);

			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO_Name||': No such NGO exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;

/